package net.sf.nwn.jme3;


import java.util.Iterator;
import java.util.LinkedHashMap;

import com.jme3.math.ColorRGBA;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;



public abstract class BaseNode
{
    protected BaseNode supernode;
    private String name;
    protected LinkedHashMap<String,BaseNode> children = new LinkedHashMap<String,BaseNode>();

    public BaseNode(BaseNode parent)
    {
        supernode = parent;
    }

    public void setSupernode(BaseNode parent)
    {
        supernode = parent;
    }

    /**
     * Gets the name.
     * @return Returns a String
     */
    public String getName()
    {
        return name;
    }

    /**
     * Sets the name.
     * @param name The name to set
     */
    public void setName(String name)
    {
        this.name = name.toLowerCase();
    }

    public String getType()
    {
        return "unknown";
    }

    public void addChild(BaseNode node)
    {
        children.put(node.getName(), node);
    }

    public boolean containsChild(String name)
    {
        return findChild(name) != null;
    }

    public BaseNode findChild(String name)
    {
        BaseNode n = (BaseNode) children.get(name);

        if (n != null)
            return n;

        Iterator<BaseNode> it = children.values().iterator();

        while (it.hasNext())
        {
            BaseNode child = (BaseNode) it.next();

            n = child.findChild(name);
            if (n != null)
                return n;
        }
        return null;
    }

    public int getDepth()
    {
        int depth = 0;

        for (BaseNode n = supernode; n != null; n = n.supernode)
            depth++;
        return depth;
    }

    public String toString()
    {
        StringBuffer sb = new StringBuffer("");
        int depth = getDepth();

        while (depth-- > 0)
            sb.append("    ");
        sb.append(getName()).append('(').append(getType());
        sb.append(')');
        Iterator<BaseNode> it = children.values().iterator();

        while (it.hasNext())
        {
            sb.append("\n").append(it.next());
        }
        return sb.toString();
    }

    public void dumpSingle(StringBuffer sb)
    {
        sb.append("node ").append(getType()).append(" ").append(getName()).append("\n");
        if (supernode == null)
        {
            sb.append("parent NULL\n");
        }
        else
        {
            sb.append("parent ").append(supernode.getName()).append("\n");
        }
    }

    public void dump(StringBuffer sb)
    {
        dumpSingle(sb);
        sb.append("endnode\n");
        Iterator<BaseNode> it = children.values().iterator();

        while (it.hasNext())
        {
            BaseNode n = (BaseNode) it.next();

            n.dump(sb);
        }
    }
    
    protected void dump(StringBuffer sb, Vector2f t)
    {
        sb.append(t.x).append(' ').append(t.y);
    }
    
    protected void dump(StringBuffer sb, Vector3f t)
    {
        sb.append(t.x).append(' ').append(t.y).append(' ').append(t.z);
    }
    
    protected void dump(StringBuffer sb, ColorRGBA t)
    {
        sb.append(t.r).append(' ').append(t.g).append(' ').append(t.b);
    }
    
    
    protected void dump(StringBuffer sb, Quaternion q) {
    	sb.append("Quaternion dump not supported");
    }

    /*
    protected void dump(StringBuffer sb, AxisAngle4f t)
    {
        sb.append(t.x).append(' ').append(t.y).append(' ').append(t.z).append(' ').append(t.angle);
    }
    */


}
